package com.example.lanouhn.bantangapp.activities.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.lanouhn.bantangapp.R;
import com.example.lanouhn.bantangapp.activities.activities.Community_Artical_Item_Activity;
import com.example.lanouhn.bantangapp.activities.contants.DT;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by lanouhn on 16/7/20.
 * 文章动态数据
 */
public class Adapter_FCommDyDt extends RecyclerView.Adapter<Adapter_FCommDyDt.MyViewHolder> {
    private Context context;
    List<DT> list;//实体类


    public Adapter_FCommDyDt(Context context, List<DT> list) {
        this.context = context;
        this.list = list;

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.community_article_fragment_item,null);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
         //设置值
        holder.tv_title.setText(list.get(position).getTitle());
        holder.tv_desc.setText(list.get(position).getDesc());
        holder.selected_item_center.setText(list.get(position).getViews());
        holder.selected_item_textright.setText(list.get(position).getComments());
        holder.tv_time.setText(list.get(position).getOrder_time_str());
        holder.tv_name.setText(list.get(position).getNickname());

        //拼接网址用的id
        final int id = list.get(position).getId();

        //图片
        Picasso.with(context).load(list.get(position).getUrl0()).into(holder.iv_mainimage);
        Picasso.with(context).load(list.get(position).getAvatar()).into(holder.iv_pic);

        /**
         * 点击跳转的方法
         */
        holder.rl_dianji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context , Community_Artical_Item_Activity.class); //跳转到Community_Artical_Item_Activity
                //把要传的id放进intent
                intent.putExtra("id" , id);
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        //找到控件
        private ImageView iv_pic;
        private ImageView iv_mainimage;
        private TextView  tv_title;
        private TextView tv_desc;
        private TextView   selected_item_center;
        private TextView   selected_item_textright;
        private TextView   tv_time;
        private TextView   tv_name;

        private RelativeLayout rl_dianji;
        public MyViewHolder(View itemView) {
            super(itemView);
            iv_pic = (ImageView) itemView.findViewById(R.id.iv_pic);
            iv_mainimage= (ImageView) itemView.findViewById(R.id.iv_mainimage);
            tv_title= (TextView) itemView.findViewById(R.id.tv_title);
            tv_desc= (TextView) itemView.findViewById(R.id.tv_desc);
            selected_item_center= (TextView) itemView.findViewById(R.id.selected_item_center);
            selected_item_textright = (TextView) itemView.findViewById(R.id.selected_item_textright);
            tv_time= (TextView) itemView.findViewById(R.id.tv_time);
            tv_name= (TextView) itemView.findViewById(R.id.tv_name);
            rl_dianji = (RelativeLayout) itemView.findViewById(R.id.rl_dianji);

        }
    }
}
